package com.tywj.lib.route

import android.app.Activity
import android.content.Context
import android.os.Parcelable
import androidx.core.app.ActivityOptionsCompat
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.callback.NavCallback
import com.alibaba.android.arouter.launcher.ARouter
import com.autocareai.lib.route.ARouterCompat
import java.io.Serializable
import java.util.*

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/8
 *     desc   : 路由导航
 *     version: 1.0.0
 * </pre>
 */
class RouteNavigation(path: String) {

    private val postcard = ARouter.getInstance().build(path)

    fun param(key: String, value: Int): RouteNavigation {
        postcard.withInt(key, value)
        return this
    }

    fun param(key: String, value: Long): RouteNavigation {
        postcard.withLong(key, value)
        return this
    }

    fun param(key: String, value: Float): RouteNavigation {
        postcard.withFloat(key, value)
        return this
    }

    fun param(key: String, value: Double): RouteNavigation {
        postcard.withDouble(key, value)
        return this
    }

    fun param(key: String, value: Boolean): RouteNavigation {
        postcard.withBoolean(key, value)
        return this
    }

    fun param(key: String, value: String): RouteNavigation {
        postcard.withString(key, value)
        return this
    }

    fun param(key: String, value: Serializable): RouteNavigation {
        postcard.withSerializable(key, value)
        return this
    }

    fun param(key: String, value: Parcelable): RouteNavigation {
        postcard.withParcelable(key, value)
        return this
    }

    fun param(key: String, value: ArrayList<out Parcelable>): RouteNavigation {
        postcard.withParcelableArrayList(key, value)
        return this
    }

    fun setFlags(flags: Int): RouteNavigation {
        postcard.withFlags(flags)
        return this
    }

    fun addFlags(flags: Int): RouteNavigation {
        postcard.addFlags(flags)
        return this
    }

    fun setAction(action: String): RouteNavigation {
        postcard.withAction(action)
        return this
    }

    fun setOptionsCompat(compat: ActivityOptionsCompat): RouteNavigation {
        postcard.withOptionsCompat(compat)
        return this
    }

    fun setTransition(enterAnim: Int, exitAnim: Int): RouteNavigation {
        postcard.withTransition(enterAnim, exitAnim)
        return this
    }

    /**
     * 开始导航
     * @param arrival 导航成功之后调用本函数
     * @return 如果是Activity路由，返回Null；如果是Fragment路由，返回该Fragment
     */
    fun navigation(context: Context, arrival: (() -> Unit)? = null): Any? {
        return ARouterCompat.navigation(
            ARouterCompat.ActivityLaunch(context),
            postcard,
            -1,
            object : NavCallback() {
                override fun onArrival(postcard: Postcard?) {
                    arrival?.invoke()
                }
            })
    }

    /**
     * 开始导航，以startActivityForResult的方式进行页面跳转
     * @param requestCode 请求码
     * @param arrival 导航成功之后调用本函数
     */
    fun navigation(activity: Activity, requestCode: Int, arrival: (() -> Unit)? = null) {
        ARouterCompat.navigation(
            ARouterCompat.ActivityLaunch(activity),
            postcard,
            requestCode,
            object : NavCallback() {
                override fun onArrival(postcard: Postcard?) {
                    arrival?.invoke()
                }
            })
    }

    /**
     * 开始导航
     * @param fragment v4包中的Fragment
     * @param arrival 导航成功之后调用本函数
     * @return 如果是Activity路由，返回Null；如果是Fragment路由，返回该Fragment
     */
    fun navigation(fragment: Fragment, arrival: (() -> Unit)? = null): Any? {
        return ARouterCompat.navigation(
            ARouterCompat.FragmentLaunch(fragment),
            postcard,
            -1,
            object : NavCallback() {
                override fun onArrival(postcard: Postcard?) {
                    arrival?.invoke()
                }
            })
    }

    /**
     * 开始导航，以startActivityForResult的方式进行页面跳转
     * @param fragment v4包中的Fragment
     * @param requestCode 请求码
     * @param arrival 导航成功之后调用本函数
     */
    fun navigation(fragment: Fragment, requestCode: Int, arrival: (() -> Unit)? = null) {
        ARouterCompat.navigation(
            ARouterCompat.FragmentLaunch(fragment),
            postcard,
            requestCode,
            object : NavCallback() {
                override fun onArrival(postcard: Postcard?) {
                    arrival?.invoke()
                }
            })
    }
}